<?php

/*
 * site/link_service.php
 *
 * Provide links management service
 *
 */

require_once( 'database_fct.php' );

Service::register( 'LinkService', 'Link Service', 'Link Management Service' );

class LinkService extends Service
{
	public function __construct()
	{
		parent::__construct();
		$this->registerMethod( 'get', array(T_INT), 'retreive a link information', 'helpless', false );
		$this->registerMethod( 'add', array(T_STR, T_STR, T_STR, TO_ARR_INT), 'add a new link', 'helpless', false );
		$this->registerMethod( 'update', array(T_INT, T_STR, T_STR, T_STR, TO_ARR_INT), 'add an existing link', 'helpless', false );
		$this->registerMethod( 'remove', array(T_INT), 'remove an existing link', 'helpless', false );
		$this->registerMethod( 'getList', array(TO_INT), 'return list of link given a labelID', 'helpless', true );
		$this->registerMethod( 'getUnlabelledList', array(), 'return list of link with no labels associated', 'helpless', true );
	}

	public function get( $id )
	{
		return SiteDBFct::getLink( $id );
	}

	public function add( $name, $url, $desc, $labelsId=null )
	{
		return SiteDBFct::addLink( $name, $url, $desc, $labelsId );
	}

	public function update( $id, $name, $url, $desc, $labelsId=null )
	{
		return SiteDBFct::updateLink( $id, $name, $url, $desc, $labelsId );
	}

	public function remove( $id )
	{
		return SiteDBFct::removeLink( $id );
	}

	public function getList( $labelID=null )
	{
		$r = SiteDBFct::listLinks( $labelID );
		// add empty "labels" field so we can reuse "LinkDesc" on client-side
		foreach( $r as $l )
		{
			$l['labels'] = array();
		}
		return array( 'links'=>$r );
	}

	public function getUnlabelledList()
	{
		$r = SiteDBFct::listUnlabelledLinks();
		// add empty "labels" field so we can reuse "LinkDesc" on client-side
		foreach( $r as $l )
		{
			$l['labels'] = array();
		}
		return array( 'links'=>$r );
	}

};

?>
